﻿Imports System.Resources
Imports System.IO
Imports System.Windows.Media.Imaging
Imports System.Windows.Data
Imports System.Windows.Media
Imports System.Windows

Public Class TypeNameToIconConverter
    Implements IValueConverter

    Private icoString As BitmapImage
    Private icoNum As BitmapImage
    Private icoDate As BitmapImage

    Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
        If targetType IsNot GetType(ImageSource) Then _
            Throw New InvalidOperationException("La cible doit être de type System.Windows.Media.ImageSource")
        Try
            Select Case CType(value, String)
                Case "String"
                    If icoString Is Nothing Then
                        icoString = createBitmapImage(New Uri("pack://application:,,,/OMANE.Common.UI.Theme;component/Images/varCar16.ico", UriKind.Absolute))
                    End If
                    Return icoString
                Case "Double"
                    If icoNum Is Nothing Then
                        icoNum = createBitmapImage(New Uri("pack://application:,,,/OMANE.Common.UI.Theme;component/Images/varNum16.ico", UriKind.Absolute))
                    End If
                    Return icoNum
                Case "DateTime"
                    If icoDate Is Nothing Then
                        icoDate = createBitmapImage(New Uri("pack://application:,,,/OMANE.Common.UI.Theme;component/Images/varDate16.ico", UriKind.Absolute))
                    End If
                    Return icoDate
                Case Else
                    Return New BitmapImage
            End Select
        Catch ex As Exception
            Return DependencyProperty.UnsetValue
        End Try
    End Function
    Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
        Throw New NotImplementedException()
    End Function

    Private Function createBitmapImage(ByVal Uri As Uri) As BitmapImage
        Dim result As BitmapImage = New BitmapImage
        result.BeginInit()
        result.UriSource = Uri
        result.EndInit()
        Return result
    End Function

End Class
